package za.ac.wits.was.disciplines;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import za.ac.wits.was.courses.Course;
import za.ac.wits.was.user.model.AcademicStaff;

/**
 *
 * @author Salman Noor : School of Electrical and Information Engineering
 * @Copyright: Wits University 2013 - All Rights Reserved
 * @email: salmannoor74415@gmail.com
 * @License: BSD License (http://www.linfo.org/bsdlicense.html)
 */
@Service
@Transactional
public class DisciplineService {

    private SessionFactory sessionFactory;

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public List<AcademicStaff> getStaffMembers(Long discpline_id) {
        Session sess = this.sessionFactory.openSession();
        Discipline theDiscipline = (Discipline) sess.get(Discipline.class, discpline_id);
        Set<AcademicStaff> as = theDiscipline.getAcademic_staff();
        List<AcademicStaff> asList = new ArrayList();
        if (as == null) {
            return asList;
        }
        for(AcademicStaff a:as){
        a.getUser_authorities();
        a.setAcademic_user(Boolean.TRUE);
        a.setFull_descriptive_name(a.getFull_descriptive_name());
        }
        sess.flush();
        sess.close();
        asList.addAll(as);
        return asList;
    }

    public List<Course> getCourses(Long discipline_id) {
        Session sess = this.sessionFactory.openSession();
        Discipline theDiscipline = (Discipline) sess.get(Discipline.class, discipline_id);
        Set<Course> cs = theDiscipline.getCourses();
        List<Course> csList = new ArrayList();
        if (cs == null) {
            return csList;
        }
        for(Course c :cs){
        c.getDiscipline();
        
        }
        csList.addAll(cs);
        sess.flush();
        sess.close();
        
        return csList;
    }

    public Discipline deleteDiscipline(Discipline existingDiscipline) {
        Session sess = this.sessionFactory.openSession();
        Set<AcademicStaff> ed = existingDiscipline.getAcademic_staff();
        Set<Course> cd = existingDiscipline.getCourses();
        if (ed != null) {
            for (AcademicStaff as : ed) {
                sess.delete(as);
            }
        }
        if (cd != null) {
            for (Course c : cd) {
                sess.delete(c);
            }
        }
        sess.delete(existingDiscipline);
        sess.close();
        return existingDiscipline;
    }

    public Discipline createNewDiscipline(Discipline newDiscipline) {
        Session sess = this.sessionFactory.openSession();
        sess.save(newDiscipline);
        sess.flush();
        sess.close();
        return newDiscipline;
    }

    public Discipline updateDiscipline(Discipline updatedDiscipline) {
        Session sess = this.sessionFactory.openSession();
        sess.saveOrUpdate(updatedDiscipline);
        sess.flush();
        sess.close();
        return updatedDiscipline;
    }

    public List<Discipline> getAllDisciplines() {
        Session sess = this.sessionFactory.openSession();
        List<Discipline> theList = sess.createQuery("from Discipline").list();
        sess.flush();
        sess.close();
        return theList;
    }
}
